#import time

import tables as tb

def h5data(turbine, h5file):
    h5file = tb.openFile((h5file), mode='r')
    turbs = h5file.root._v_children
    turb = turbs[turbine]
    return turb

def filter_h5data(turbine_data, filt_strings, startnum=None, steplen=None, stopnum=-1):
    for i, filt in enumerate(filt_strings):
        if i == 0:
            filt_string = filt
        else:
            filt_string += " & " + filt
    filt_data = turbine_data.readWhere(filt_string, start=startnum, stop=stopnum, step=steplen)
    return filt_data

def from_turbines(turbines=["WH1115"], h5file="../../data/bin/turbines.h5"):
    data_sets = []
    for turb in turbines:
        data = h5data(turbine=turb, h5file=h5file)
        data_sets.append(data)
    return data_sets

def main():
    turb = from_turbines()[0]
    print "Num of samples before filter: ",
    print len(turb)
    filt_strings = ["(ActivePowerAvg > 200)", "(ActivePowerAvg < 2200)"]
    f_data = filter_h5data(turb, filt_strings)
    print "Num of samples after filter: ",
    print len(f_data)
    return

if __name__=='__main__':
    main()
